<template>
	<view class="content">
		<image class="banner" src="/static/image/banner.png"></image>
		<view class="content-bg">
			<view class="content-box">
				<view class="entry-box">
					<view class="entry-item" @click="navigateToBallroomIntro">
						<image class="entry-icon" src="/static/image/icon_pp.png"></image>
						<text>球室介绍</text>
					</view>
					<view class="entry-item" @click="openAppointmentModal">
						<image class="entry-icon" src="/static/image/icon_book .png"></image>
						<text>预约须知</text>
					</view>
					<view class="entry-item" @click="navigateToMyReservation">
						<image class="entry-icon" src="/static/image/icon_appointment.png"></image>
						<text>我的预约</text>
					</view>
				</view>
				<view class="info">
					<view class="info-item place">
						<image class="place-icon" src="/static/image/icon_place.png"></image>
						<text>场馆地址 | 何家湖校区图书馆1楼内</text>
					</view>
					<view class="info-item appointment-time">
						<image class="place-icon" src="/static/image/icon_time.png"></image>
						<text>预约时间: <text class="time-color">{{ appointment_time }}</text></text>
					</view>
				</view>
				<view class="list-title">
					<view class="title-left">球桌列表</view>
					<view class="title-right" @click="navigateToList">
						<text>查看更多</text>
						<image class="icon" src="/static/image/arrow_right.png"></image>
					</view>
				</view>
				<view class="list-box">
					<!-- 修复：循环变量名错误和方法名错误 -->
					<view class="list-item" v-for="item in table_Data.slice(0, 2)" :key="item.id">
						<image :src="item.imageUrl" class="table-img"></image>
						<view class="item-info">
							<text>名称：{{ item.name }}</text>
							<text>价格：¥{{ item.price.toFixed(2) }} 元/小时</text>
							<text>可预约时间：{{ getHoursAndMinutes(item.starApptTime) }}-{{ getHoursAndMinutes(item.endApptTime) }}</text>
							<view @click="goToReservation(item.id)">预定</view>
						</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 预约须知弹窗 -->
		<view class="modal" v-if="showModal">
			<view class="modal-content">
				<view class="modal-header">
					<text class="modal-title">预约须知</text>
					<view class="close-btn" @click="closeAppointmentModal">×</view>
				</view>
				<view class="modal-body">
					<view class="notice-content">
						<text class="notice-text">1. 本场馆开放时间: 8:00-22:00</text>
						<text class="notice-text">2. 预约需提前1小时以上，可预约未来7天内的场地</text>
						<text class="notice-text">3. 每次预约时长不超过2小时</text>
						<text class="notice-text">4. 请按时到场使用，如需取消请提前30分钟</text>
						<text class="notice-text">5. 使用时请爱护设施，损坏需照价赔偿</text>
						<text class="notice-text">6. 禁止携带食物进入场地，保持场地清洁</text>
						<text class="notice-text">7. 请穿着合适的运动服装和鞋子</text>
					</view>
				</view>
				<view class="modal-footer">
					<button class="confirm-btn" @click="closeAppointmentModal">确定</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import { ref, onMounted } from 'vue';

	const appointment_time = ref('19:00 - 21:00');
	const showModal = ref(false);
	import { getProductList } from '@/services/product.js';
	// 修复：变量名拼写错误（table_Date → table_Data）
	const table_Data = ref([])

	//格式化可预约时间
	const getHoursAndMinutes = (t) => t?.match(/^\d{1,2}:\d{1,2}/)?.[0].padStart(5, '0') || t;

	const getList = async () => {
		try {
			const list = await getProductList()
			console.log(list)
			if (list.code === 200) {
				table_Data.value = list.data?.list ?? []
			} else {
				uni.showToast({
					title: list.message || '获取数据失败',
					icon: 'none'
				})
			}
		} catch (error) {
			console.error('获取球桌列表失败:', error)
			uni.showToast({
				title: '网络异常，请稍后重试',
				icon: 'none'
			})
		}
	}

	// 在页面挂载时获取数据
	onMounted(() => {
		getList()
	})

	// 跳转到列表页
	const navigateToList = () => {
		uni.switchTab({
			url: '/pages/list/list'
		});
	};

	// 跳转到球室介绍页面
	const navigateToBallroomIntro = () => {
		uni.navigateTo({
			url: '/pages/index/ballroomIntroduction'
		});
	};

	// 跳转到我的预约页面
	const navigateToMyReservation = () => {
		uni.navigateTo({
			url: '/pages/myReservation/myReservation' // 假设的路径，根据实际情况修改
		});
	};

	// 打开预约须知弹窗
	const openAppointmentModal = () => {
		showModal.value = true;
	};

	// 关闭预约须知弹窗
	const closeAppointmentModal = () => {
		showModal.value = false;
	};

	// 修复：实现缺失的预定跳转方法
	const goToReservation = (tableId) => {
		if (!tableId) {
			uni.showToast({
				title: '参数错误',
				icon: 'none'
			})
			return;
		}
		uni.navigateTo({
			url: `/pages/list/appointmentDetails?tableId=${tableId}`
		})
	}

	// 移除未使用的冗余方法
</script>

<style lang="scss">
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		background-color: #f5f5f5;
		min-height: 100vh;

		.banner {
			width: 100%;
			height: 160px;
			object-fit: cover; // 防止图片拉伸
		}

		.content-bg {
			width: 100%;
		}

		.content-box {
			padding: 0 15px;
			padding-top: 20px;
		}

		.entry-box {
			background-color: #fff;
			border-radius: 15px;
			box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.25);
			display: flex;
			justify-content: space-around;
			padding: 12px;
			margin-bottom: 15px;
		}

		.entry-item {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			cursor: pointer; // 增加点击提示
		}

		.entry-icon {
			width: 35px;
			height: 35px;
			margin-bottom: 5px;
		}

		.list-title {
			margin: 20px 0;
			display: flex;
			justify-content: space-between;
		}

		.title-left {
			display: flex;
			align-items: center;

			&::before {
				content: '';
				display: inline-block;
				width: 4px;
				height: 16px;
				background: rgba(81, 89, 245, 1);
				border-radius: 20px;
				margin-right: 5px;
			}
		}

		.title-right {
			display: flex;
			align-items: center;
			cursor: pointer; // 增加点击提示

			.icon {
				width: 15px;
				height: 15px;
				margin-left: 5px;
			}
		}

		.list-box {
			.list-item {
				background-color: #fff;
				border-radius: 10px;
				display: flex;
				flex-direction: row;
				padding: 15px;
				margin-bottom: 15px;

				.table-img {
					width: 100px;
					height: 100px;
					border-radius: 8px;
					margin-right: 15px;
					object-fit: cover; // 防止图片拉伸
				}

				.item-info {
					flex: 1;
					display: flex;
					flex-direction: column;
					justify-content: space-between;

					text:nth-child(1) {
						font-size: 18px;
						font-weight: bold;
					}

					text:nth-child(2) {
						color: #ff6666;
						font-size: 16px;
					}

					view {
						align-self: flex-end;
						background-color: #3b82f6;
						color: white;
						padding: 5px 15px;
						border-radius: 20px;
						cursor: pointer;
					}
				}
			}
		}

		.info {
			margin: 20px 0;

			.info-item {
				background-color: #fff;
				border-radius: 8px; // 优化圆角
				margin-bottom: 10px;
				padding: 10px 15px;
				display: flex;
				align-items: center;

				image {
					margin-right: 10px;
				}

				&.place {
					image {
						width: 23px;
						height: 23px;
						margin-right: 8px;
					}
				}

				&.appointment-time {
					image {
						width: 20px;
						height: 20px;
					}
				}

				.time-color {
					color: rgb(249, 156, 17);
				}
			}
		}

		.modal {
			position: fixed;
			top: 0;
			left: 0;
			right: 0;
			bottom: 0;
			background-color: rgba(0, 0, 0, 0.6);
			display: flex;
			justify-content: center;
			align-items: center;
			z-index: 999;

			.modal-content {
				background-color: #fff;
				border-radius: 10px;
				width: 70%;
				max-width: 400px;
				padding: 20px;

				.modal-header {
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin-bottom: 20px;
					padding-bottom: 10px;
					border-bottom: 1px solid #eee;

					.modal-title {
						font-size: 18px;
						font-weight: bold;
					}

					.close-btn {
						font-size: 24px;
						cursor: pointer;
					}
				}

				.modal-body {
					.notice-content {
						.notice-text {
							display: block;
							margin-bottom: 10px;
						}
					}
				}

				.modal-footer {
					display: flex;
					justify-content: flex-end;
					margin-top: 20px;

					button {
						padding: 8px 20px;
						border-radius: 5px;
						border: none;
						cursor: pointer;

						&.confirm-btn {
							background-color: #3b82f6;
							color: white;
						}
					}
				}
			}
		}
	}
</style>